package com.example.dictionary.controller;

import com.example.dictionary.service.GithubDownloadService;
import com.example.memento.model.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * GitHub 仓库下载控制器
 */
@RestController
@RequestMapping("/api/github")
public class GithubDownloadController {
    
    private static final Logger log = LoggerFactory.getLogger(GithubDownloadController.class);
    
    private final GithubDownloadService githubDownloadService;
    
    public GithubDownloadController(GithubDownloadService githubDownloadService) {
        this.githubDownloadService = githubDownloadService;
    }
    
    /**
     * 下载 DomainWordsDict 仓库
     * 
     * @param targetDir 目标目录（可选，默认为 data/res）
     * @param extract 是否解压（可选，默认为 true）
     */
    @PostMapping("/download/domain-words-dict")
    public ResponseEntity<ApiResponse<Map<String, Object>>> downloadDomainWordsDict(
            @RequestParam(required = false) String targetDir,
            @RequestParam(required = false, defaultValue = "true") boolean extract) {
        
        try {
            ApiResponse<Map<String, Object>> response = githubDownloadService
                    .downloadDomainWordsDict(targetDir, extract);
            
            if (response.isSuccess()) {
                return ResponseEntity.ok(response);
            } else {
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            log.error("下载 DomainWordsDict 失败", e);
            ApiResponse<Map<String, Object>> errorResponse = ApiResponse.<Map<String, Object>>builder()
                    .success(false)
                    .message("下载失败: " + e.getMessage())
                    .data(null)
                    .build();
            return ResponseEntity.internalServerError().body(errorResponse);
        }
    }
    
    /**
     * 下载指定的 GitHub 仓库
     * 
     * @param request 下载请求
     */
    @PostMapping(value = "/download", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ApiResponse<Map<String, Object>>> downloadRepository(
            @RequestBody GithubDownloadRequest request) {
        
        try {
            ApiResponse<Map<String, Object>> response = githubDownloadService
                    .downloadRepository(
                            request.gitUrl,
                            request.branch,
                            request.targetDir,
                            request.extract
                    );
            
            if (response.isSuccess()) {
                return ResponseEntity.ok(response);
            } else {
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            log.error("下载 GitHub 仓库失败: {}", request.gitUrl, e);
            ApiResponse<Map<String, Object>> errorResponse = ApiResponse.<Map<String, Object>>builder()
                    .success(false)
                    .message("下载失败: " + e.getMessage())
                    .data(null)
                    .build();
            return ResponseEntity.internalServerError().body(errorResponse);
        }
    }
    
    /**
     * 下载请求模型
     */
    public static class GithubDownloadRequest {
        public String gitUrl;
        public String branch;
        public String targetDir;
        public Boolean extract;
    }
}

